package com.bonc.rms.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.bonc.rms.entity.AgeTemplate;
import com.bonc.rms.entity.DegreeTemplate;
import com.bonc.rms.entity.GenderTemplate;
import com.bonc.rms.entity.WorkingExperienceTemplate;
import com.bonc.rms.utils.IESSearchUtil;
import com.bonc.rms.utils.impl.ESSearchUtilImpl;

/**
 * 饼状图控制器
 * @author zlq
 *
 */
@RestController(value = "pieChartShow")
@RequestMapping(value = "/pieChartShow", method = RequestMethod.GET)
public class PieChartShowController {

	/**
	 * 学位 
	 */
	@RequestMapping(value = "/degree")
	public String showDegree(){
		
		Map<String, Object> degreeMap = new HashMap<String,Object>();		
		List<DegreeTemplate> degrees = new LinkedList<>();		
		IESSearchUtil iesSearchUtil = new ESSearchUtilImpl();
		Map<String, Double> resultMap = iesSearchUtil.termAgg("degree").get(0);
		Iterator<String> iterator = resultMap.keySet().iterator();
		while(iterator.hasNext()){
			String str = iterator.next();
			DegreeTemplate degree = new DegreeTemplate();
			degree.setName(str);
			degree.setValue(resultMap.get(str) == null ? 0.0 : resultMap.get(str));
			degrees.add(degree);
		}		
		degreeMap.put("title","学位");
		degreeMap.put("degree", degrees);
		return JSON.toJSONString(degreeMap);
	}
	
	/**
	 * 性别
	 */
	@RequestMapping(value = "/gender")
	public String genderShow(){
		
		Map<String, Object> genderMap = new HashMap<String,Object>();		
		List<GenderTemplate> genders = new LinkedList<>();
		IESSearchUtil iesSearchUtil = new ESSearchUtilImpl();
		Map<String, Double> resultMap = iesSearchUtil.termAgg("gender").get(0);
		Iterator<String> iterator = resultMap.keySet().iterator();
		while(iterator.hasNext()){
			String str = iterator.next();
			GenderTemplate gender = new GenderTemplate();
			gender.setName(str);
			gender.setValue(resultMap.get(str) == null ? 0.0 : resultMap.get(str));
			genders.add(gender);
		}		
		genderMap.put("title", "性别");
		genderMap.put("gender", genders);
		return JSON.toJSONString(genderMap);
	}
	 
	/**
	 * 年龄
	 */
	@RequestMapping(value = "/age")
	public String ageShow(){
		
		Map<String, Object> ageMap = new HashMap<String,Object>();
		List<String> nameList = new LinkedList<>();
		List<Long> valueList = new LinkedList<>();
		List<AgeTemplate> ages = new LinkedList<>();
		nameList.add("25岁以下");
		nameList.add("35岁以上");
		nameList.add("25-35岁");		
		IESSearchUtil iesSearchUtil = new ESSearchUtilImpl();
		Map<String, Long> resultMap = iesSearchUtil.rangeAgg("age", 25, 35);
		Iterator<String> iterator = resultMap.keySet().iterator();
		while(iterator.hasNext()){
			String str = iterator.next();
			valueList.add(resultMap.get(str) == null ? 0 : resultMap.get(str));
		}	
		for(int i = 0; i < nameList.size(); i++){
			String name = nameList.get(i);
			Long value = valueList.get(i);
			ages.add(new AgeTemplate(name,value));
			
		}
		ageMap.put("title", "年龄");
		ageMap.put("age", ages);
		return JSON.toJSONString(ageMap);
	}
	
	/**
	 * 工作经验
	 */
	@RequestMapping(value = "/workingExperience")
	public String workingExperienceShow(){
		
		Map<String, Object> workingExperienceMap = new HashMap<String,Object>();
		List<String> nameList = new LinkedList<>();
		List<Long> valueList = new LinkedList<>();
		List<WorkingExperienceTemplate> workingExperiences = new LinkedList<>();
		nameList.add("3年以下");
		nameList.add("7年以上");
		nameList.add("3-7年");		
		IESSearchUtil iesSearchUtil = new ESSearchUtilImpl();
		Map<String, Long> resultMap = iesSearchUtil.rangeAgg("workingExperience", 3, 7);
		Iterator<String> iterator = resultMap.keySet().iterator();
		while(iterator.hasNext()){
			String str = iterator.next();
			valueList.add(resultMap.get(str) == null ? 0 : resultMap.get(str));
		}	
		for(int i = 0; i < nameList.size(); i++){
			String name = nameList.get(i);
			Long value = valueList.get(i);
			workingExperiences.add(new WorkingExperienceTemplate(name,value));			
		}
		workingExperienceMap.put("title", "工作经验");
		workingExperienceMap.put("workingExperience", workingExperiences);
		return JSON.toJSONString(workingExperienceMap);
	}

}
